package com.foreveredu.propms.persist.hibernate;

import java.util.List;

import com.foreveredu.propms.domain.Building;
import com.foreveredu.propms.domain.House;
import com.foreveredu.propms.domain.PageModel;
import com.foreveredu.propms.persist.IBuildingDao;

public class HibernateBuildingDao extends
		HibernateAbstractDao<Building, Integer> implements IBuildingDao {

	private static final long serialVersionUID = 1L;

	public HibernateBuildingDao() {
		super(Building.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Building findByBuildingId(String buildingId) {
		List<Building> buildingList = this.getHibernateTemplate().find(
				"from Building b where b.buildingId=?", buildingId);
		Building building = null;
		if (buildingList != null && buildingList.size() > 0) {
			building = buildingList.get(0);
		}
		return building;
	}

	/*
	 * @param districtId
	 * 0代表搜索所有物业服务处的楼栋
	 * 否则，不为0代表搜索当前物业服务处的楼栋
	 */
	@SuppressWarnings("unchecked")
	@Override
	public PageModel findBuildingList(int offset,int districtId) {
		List<Building> buildings = null;
		if (districtId == 0) {
			buildings = this.getHibernateTemplate().find(
					"from Building b order by b.id");
		} else {
			buildings = this.getHibernateTemplate().find(
					"from Building b where b.subDistrict.district.id=? order by b.id",districtId);
		}
		PageModel pm = new PageModel();
		if (buildings != null && buildings.size() > 0) {
			pm.setTotal(buildings.size());
			if (offset + 10 > buildings.size()) {
				pm.setDatas(buildings.subList(offset, buildings.size()));
				return pm;
			}
			pm.setDatas(buildings.subList(offset, offset + 10));
		}
		return pm;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Building> findBuildingsBySubDistrict(int id) {
		String hql = "from Building b where b.subDistrict.id=? ";
		List<Building> buildingList = this.getHibernateTemplate().find(hql, id);
		return buildingList;
	}

}
